package 滑动窗口And双指针.双指针;

import org.junit.Test;

import java.util.Arrays;

/**
 * @author aodre , QQ : 480029069
 * @date 2023/7/10 15:59
 */
public class lc454四数之和II {


    public int solve(int nums1[], int nums2[], int nums3[], int nums4[]) {
        int ans = 0;
        int len = nums1.length;
        Arrays.sort(nums4);
        for (int i = 0; i < len; i++) {
            for (int j = 0; j < len; j++) {
                for (int k = 0; k < len; k++) {
                ans += binary(nums4,nums1[i] + nums2[j] + nums3[k]);
                }
            }
        }
        return ans;
    }

    public int binary(int nums[], int val) {
        val = -val;
        int l = 0, r = nums.length - 1, index = -1;
        while (l <= r) {
            int mid = l + r >> 1;
            if (nums[mid] == val) {
                index = mid;
                break;
            } else if (nums[mid] > val) {
                r = mid - 1;
            } else {
                l = mid + 1;
            }
        }
        if (index == -1) {
            return 0;
        }
        int count = 1, l_ = index, r_ = index;
        while (r_ < nums.length - 1) {
            if (nums[r_] != nums[r_ + 1]) {
                break;
            }
            count++;
            r_++;
        }
        while (l_ > 0) {
            if (nums[l_] != nums[l_ - 1]) {
                break;
            }
            count++;
            l_--;
        }
        return count;
    }

    @Test
    public void test(){
        int num1[] = {0};
        int num2[] = {0};
        int num3[] = {0};
        int num4[] = {0};
        System.out.println(solve(num1,num2,num3,num4));
    }


}
